package com.kaaction.modules.mer.api;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.kaaction.common.web.BaseController;
import com.kaaction.modules.mer.entity.Address;
import com.kaaction.modules.mer.service.AddressService;

@Controller
@RequestMapping(value = "/api/v1/addressApi")
public class AddressApiController extends BaseController{
	
	@Autowired
	AddressService addressService;
	
	/**
	 * 新增地址
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 */
	@RequestMapping(value = "/address")
	public void spuClassify(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException {
		
		Address addr = new Address();
		
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String mobile = request.getParameter("mobile");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String county = request.getParameter("county");
		String address = request.getParameter("address");
		String isDefault = request.getParameter("isDefault");
		String thirdpartyId = request.getParameter("thirdpartyId");
		
		addr.setId(id);
		addr.setName(name);
		addr.setMobile(mobile);
		addr.setProvince(province);
		addr.setCity(city);
		addr.setCounty(county);
		addr.setAddress(address);
		addr.setIsDefault(isDefault);
		addr.setThirdpartyId(thirdpartyId);
		addressService.updOrInsAddress(addr);
		super.writeAjaxResponse(response,"");
	}
	
	/**
	 * 删除地址
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 */
	@RequestMapping(value = "/delAddress")
	public void delAddress(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException {
		
		Address  address =  new Address(); 
		String id = request.getParameter("id");
		address.setId(id);
		addressService.delete(address);
		
		String thirdpartyId = request.getParameter("thirdpartyId");
		String isDefault = request.getParameter("isDefault");
		if("1".equals(isDefault)){
			address.setThirdpartyId(thirdpartyId);
			addressService.updateAddr(address);
		}
		super.writeAjaxResponse(response,"");
	}
	
	/**
	 * 查询地址
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 */
	@RequestMapping(value = "/selAddress")
	public void selAddress(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException {
		String thirdpartyId = request.getParameter("thirdpartyId");
		Address  address =  new Address(); 
		address.setThirdpartyId(thirdpartyId);
		super.writeAjaxResponse(response,addressService.selAddress(address));
	}
	
	/**
	 * 更新初始化
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 */
	@RequestMapping(value = "/getAddress")
	public void getAddress(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException {
		String id = request.getParameter("id");
		Address  address =  new Address(); 
		address.setId(id);
		super.writeAjaxResponse(response,addressService.get(address));
	}
	
	/**
	 * 取消默认
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 */
	@RequestMapping(value = "/cancelAddress")
	public void cancelAddress(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException {
		String isDefault = request.getParameter("isDefault");
		if("1".equals(isDefault)){
			String id = request.getParameter("id");
			String thirdpartyId = request.getParameter("thirdpartyId");
			Address  address =  new Address(); 
			address.setId(id);
			address.setThirdpartyId(thirdpartyId);
			addressService.cancelAddress(address);
			super.writeAjaxResponse(response,"");
		}else{
			writeFailResponse(response,"","不是默认地址");
		}
		
	}
	
	

}
